package com.grm.sys.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.grm.common.entity.Result;
import com.grm.common.annotation.HasPerm;
import com.grm.common.annotation.Log;
import com.grm.sys.model.SysPost;
import com.grm.sys.service.SysPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sys/post")
public class SysPostController {
    @Autowired
    private SysPostService sysPostService;

    @GetMapping("query/all")
    public Result queryAll() {
        List<SysPost> sysPosts = sysPostService.queryAll();
        return Result.success(sysPosts);
    }

    @PostMapping("/list")
    public Result queryListSelective(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestBody SysPost queryParam) {
        // 开启分页
        PageHelper.startPage(pageNum, pageSize);
        List<SysPost> sysPosts = sysPostService.queryListSelective(queryParam);
        PageInfo<SysPost> pageInfo = new PageInfo<>(sysPosts);
        return Result.success(pageInfo.getList(), pageInfo.getTotal());
    }

    @Log("添加岗位")
    @HasPerm("sys:post:add")
    @PostMapping("/add")
    public Result add(@RequestBody SysPost sysPost) {
        sysPostService.add(sysPost);
        return Result.success();
    }

    @Log("修改岗位")
    @HasPerm("sys:post:update")
    @PutMapping("/update")
    public Result update(@RequestBody SysPost sysPost) {
        sysPostService.update(sysPost);
        return Result.success();
    }

    /**
     * 删除岗位
     */
    @Log("删除岗位")
    @HasPerm("sys:post:delete")
    @PostMapping("/delete")
    public Result delete(@RequestBody List<Long> ids) {
        sysPostService.delete(ids);
        return Result.success();
    }
}
